﻿/* File Created: августа 28, 2012 */
//Инициализация
function vmInit() {
    this.name = 'modelInit';
    this.key = '';
    this.data = { progress: '0', isFinished: 'false' };
}

//Авторизация
function vmAuth(model) {
    var $this = this;
    $this.name = 'modelAuth';
    if (model == null) {
        $this.data = {
            title: 'Please enter your user name and password.',
            user: ko.observable('Shakh Denis'),
            pass: ko.observable(''),
            auto: ko.observable(true),
            error: ''
        };
    }
    else {
        $this.data = {
            title: model.title,
            user: ko.observable(model.user),
            pass: ko.observable(model.pass),
            auto: ko.observable(model.auto),
            error: model.error
        };
    }

    $this.key = ['user', 'pass'];
    $this.getActionList = function () {
        //Сформируем запрос (сначала вручную)
        var req = new Object();
        req.name = 'getActionList'
        req.key = mk.getKey();
        req.data = '';
        mk.doRequest(req);
    }

    $this.login = function () {
        if (!mk.isOnline) {

        }
        else {
            getAuthorization({ userName: $this.data.user(), password: $this.data.pass(), auto: $this.data.auto() }, $this.loginSuccess, $this.loginFailed);
        }
    }

    $this.loginSuccess = function (data, status) {
        var newViewModel;
        if (data.d.Name == "modelAuth") {
            newViewModel = new vmAuth({ title: data.d.Title, user: data.d.User, pass: data.d.Password, auto: data.d.Auto, error: data.d.Error });
        }
        else if (data.d.Name == "modelList") {
            var newModel = { title: data.d.Title, items: data.d.Items, showRenderTimes: data.d.ShowRenderTimes };
            newViewModel = new vmObjectList(newModel);
        }

        mk.setViewModel(newViewModel);
        mk.loadModelTemplate(templateViews)
    }

    $this.loginFailed = function (request, status, error) {
        log.error(error);
    }
}

// Элемент списка объектов
var objectListItem = function (key) {
    this.key = key;
}

// Список объектов
function vmObjectList(model) {
    var $this = this;
    $this.name = 'modelList';
    $this.key = '';

    if (model == null) {
        $this.data = {
            title: 'Object list',
            items: ko.observableArray([]),
            showRenderTimes: ko.observable(true)
        };
    }
    else {
        $this.data = {
            title: model.title,
            items: ko.observableArray(model.items),
            showRenderTimes: ko.observable(model.showRenderTimes)
        };
    }
}